<?
namespace Api
{
    /**
     * Class designed to manage dynamic htaccess 
     */
     class Htaccess extends \Api\Model\DB2
     {
        /**
         * Overwrite htaccess file, from table `htaccess`.          
         * Htaccess file shoudl have defined section:
         *
         * #dynamic
         * #end
         *
         *
         * @param string $path - path to htaccess file         
         * @return bool
         */
        public function Overwrite($path)
        {                        
           //  if(!file_exists($path)) return false;            
            header('Content-type: text/plain');       
            $file = fopen($path, "r");
            $data = fread($file, filesize($path));
            fclose($file);
            
            $syntax = array();
            $syntax[0] = strpos($data, "#dynamic")+strlen("#dynamic");
            if($syntax[0] === false)
            {
                echo "error";
                return false;
            }
            
            $syntax[1] = strpos($data, "#end", $syntax[0]);
            if($syntax[1] === false)
            {
                echo "error";
                return false;
            }
            
            $newData = substr($data, 0, $syntax[0])."\n";
            
            foreach($this->getURL() as $k => $v)
            {
                
                $newData.='RewriteRule'."\t".'^'.$v['module'].'/'.$v['mask'].
                ($v['allow_param']==1?',(.*)':'').
                '$ '."\t".' public/index.php?m='.$v['module'].'&c='.$v['controller'].'&a='.$v['action'].
                ($v['allow_param']==1?'&$1':'').' [L] '."\n";
                
            }
            
            $newData.=substr($data, $syntax[1])."\n";            
            echo $newData;
            $file = fopen($path, "w");
            fwrite($file, $newData);
            fclose($file);
            
            return true;
        }
        
        /**
         * Return all link data from `htaccess` table
         *
         * @return array
         */
        public function getURL()
        {
            $Q = $this->db->query("SELECT * FROM `htaccess`");
            if( false !== ( $res = $Q->fetchAll()))
            {
                return $res;
            }
            return array();
        }
        
        /**
         * Looking for matching url, if exists return all data
         *
         * @param array $url(module,controller,action)
         * @return array
         */
        public function matchingURL(array $url)
        {
            $Q = $this->db->prepare("SELECT * FROM `htaccess` WHERE `module`=? AND `controller`=? AND `action`=?");
            foreach($url as $k => $v)
            {
                $Q->bindValue($k+1,$v,\PDO::PARAM_STR);
            }
            $Q->execute();
            
            if(false !== ( $res = $Q->fetch()) )
            {
                return $res;
            }/**/
            return false;
        }
     }
}
?>